package com.hdoit.kylin.logical.user.service.impl;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.kylin.logical.user.dao.ITeacherDao;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.StringHelper;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月20日 18:00:15
* @comment
*/
@Component
public class TeacherServiceImpl extends SuperService<Teacher> implements ITeacherService{
	private IEmployeeService employeeService;
	private IRoleService roleService;
	private ITeacherDao teacherDao;
	
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}


	public ITeacherDao getTeacherDao() {
		return teacherDao;
	}

	@Resource
	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	@Override
	public boolean applyChange(Teacher teacher) {
		if(employeeService.applyEmployeeLogin(teacher)){
			if(ModelStatus.add.equals(teacher.getModelStatus())){
				List<String> roleIdList=roleService.queryListStringByMark("teacher");
				if(!employeeService.applyRoleIds(teacher, StringHelper.join(roleIdList))){
					throw new DaoBaseRuntimeException("applyRoleIds roll back");
				}
			}
			if(!super.applyChange(teacher)){
				throw new DaoBaseRuntimeException("teacher applyChange roll back");
			}
			
			return true;
		}
		return false;
	}

	@Override
	public List<Teacher> queryListByGroupId(SearchModel search,PagerModel pagerModel,String groupId) {
		if(StringUtils.isEmpty(groupId))
			return queryList(search,pagerModel);
		return teacherDao.queryListByGroupId(search,pagerModel,groupId);
	}

	@Override
	public String queryGroupIdByLoginId(String loginId) {
		Teacher teacher=queryModel(loginId);
		return null==teacher?null:teacher.getGroupId();
	}

	@Override
	public int queryCountByGroupId(SearchModel search, String groupId) {
		if(StringUtils.isEmpty(groupId))
			return queryCount(search);
		return teacherDao.queryCountByGroupId(search, groupId);
	}

	@Override
	public List<String> queryListStringByGroupId(String groupId) {
		return teacherDao.queryListStringByGroupId(groupId);
	}
	
	@Override
	public List<Teacher> queryListByGroupId(String groupId) {
		return teacherDao.queryListByGroupId(groupId);
	}
}
